const app = getApp(),url = app.globalData.url;
var WxParse = require('../wxParse/wxParse.js');
import { EnumUserType } from '../../utils/constant.js'
Page({
  data: {
    status:1,//招聘会
    sx:1,
    num:0,
    hasJoin: false,
    zhiwei:{
      "address": "string",
      "companyName": "string",
      "currentPage": 1,
      "pageSize": 10,
      "positionName": ""//职位名称
    },
    qid:'',//企业id
    nid:'',//分类按钮状态 234
    enterprise_num:'',
    position_num:'',
    elAppls:'',//个人用户审核状
    jlzt: '', // 个人用户简历状态
    detail:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    let { id, status, nid} = options
    this.userType = wx.getStorageSync('userType')
    if(this.userType === EnumUserType.QiYe){
      this.userUid = wx.getStorageSync('userInfo').enterpriseUid
      this.getJoinStatus(id)
    } else {
      this.userUid = wx.getStorageSync('user_massage').userUid
      this.getCVBaseInfo()
    }
    this.setData({zphid: id,status: status,nid: nid})
    wx.request({
      url:url+'jobFair/getDetailJobFairId',
      method: "GET",
      data:{
        job_fair_id :options.id,
        userUid: this.userUid
      },
      success(res){
        var ret = res.data
        if(ret.code == 200){
          that.setData({
            detail:ret.data,
            qid:ret.data.id,
          })
          var content = ret.data.content
          WxParse.wxParse('article', 'html',content, that, 5);
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
    setTimeout(function(){
      that.zwlist()
      that.canhuiqiye(options.id)
      that.shuliang(options.id)
    },800)
  },
  getJoinStatus(zphid){
    let params ={
      enterpriseUid: this.userUid,
      jfId: zphid
    }
    let _this = this
    wx.request({
      url: url + 'jobFair/searchMyinZph',
      data: params,
      method: "GET",
      success(res){
        _this.setData({
          status: res.data ? 2 : 1 // 2已报名 1未报名
        })
      },
    })
  },
  getCVBaseInfo(){
    let _this = this
    wx.request({
      url: url + 'cvBaseInfo/getCVBaseInfoByUserUid?user_uid='+ this.userUid,
      method: "GET",
      success(res){
        var ret = res.data
        if(ret.code != 200 || !ret.data){
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
          return
        }
        _this.setData({
          elAppls: ret.data.elAppls,
          jlzt: ret.data.jlzt
        })
      }
    })
  },
  // 数量
  shuliang(id){
    var that = this
    var enterprise_uid = that.data.enterprise_uid
    wx.request({
      url:url+'jobFair/getJobFairQuantity',
      method: "GET",
      data:{
        job_fair_id:id,
        enterprise_uid :enterprise_uid,
      },
      success:function(res){
        console.log("数量",res);
        var ret = res.data
        if(ret.code == 200){
          that.setData({
            enterprise_num:ret.data.enterprise_num,
            position_num:ret.data.position_num
          })
        }else{
          // wx.showToast({
          //   title: ret.msg,
          //   icon:'none'
          // })
        }
      }
    })
  },
  canhuiqiye(id){
    var that = this
    // 参会企业
    wx.request({
      url:url+'jobFair/getJFAllEnterprise',
      method: "GET",
      data:{
        jf_id:id,
        pageNum:1,
        pageSize:15
      },
      success:function(res){
        console.log("参会企业",res);
        var ret = res.data
        if(ret.code == 200){
          that.setData({
            qiyearr:ret.data.content,
            qiye:ret.data
          })
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  // 职位列表
  zwlist(){
    var that = this
    var jfid = that.data.zphid
    wx.request({
      // url:url+'detail/getAllJFEnterpriseDetailPositionByPage',
      url:url+'jobFair/getJFHotPosition',
      method: "GET",
      data:{
        pageNum:1,
        pageSize:150,
        position_name:'',
        jf_id: jfid,

      },
      success:function(res){
        console.log("招liebaio ",res);
        var ret = res.data
        if(ret.code == 200){
          that.setData({
            listdata:ret.data.content,//,
            num:ret.data.content.length//
          })
          
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  detail(e){
    let did = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/HomePage/workdetail/workdetail?id='+did,
    })
  },
  // 报名
  baoming(e){
    if(this.userType === EnumUserType.QiYe){
      let { detail} = this.data
      debugger
      if(detail.zphJzrq && new Date(detail.zphJzrq) < new Date()){
        wx.showToast({ 
          title: '招聘会报名已截止，不能报名招聘会', 
          icon:'none'
        })
        return
      }
      wx.navigateTo({
        url: '/Company/meetjoin/join?id='+ detail.id +'&address='+ detail.address
      })
      return
    }
    let {elAppls, jlzt} = this.data
    if (!elAppls || elAppls != 2) {
      wx.showToast({ 
        title: '用户未通过审核，不能申请职位', 
        icon:'none'
      })
      return
    }
    if (jlzt != 1) {
      wx.showToast({ 
        title: '简历异常，请完善简历', 
        icon:'none'
      })
      return
    }
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认执行此操作？',
      success (res) {
        if (res.confirm) {
          wx.request({
            url:url+'jobFair/personalJoinJopFair',
            method: "POST",
            data:{
              jobFairId:that.data.qid,
              joinStatus:2,
              userUid: that.userUid
            },
            success:function(res){
              var ret = res.data
              if(ret.code == 200){
                that.setData({
                  status:2
                })
                wx.showToast({
                  title: '报名成功',
                  icon:'none',
                })
              }else{
                wx.showToast({
                  title: '请添加个人简历',
                  icon:'none',
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 更多企业
  more(){
    var zphid = this.data.zphid
    wx.navigateTo({
      url: './allqiye/allqiye?zphid=' + zphid,
    })
  },
  // 切换
  butt(e){
    var that =this
    var sx = e.currentTarget.dataset.tab
    console.log(sx)
    that.zwlist()
    that.setData({sx})
  },
  // 搜索
  searchBut:function(e){
    var that = this
    let status = e.detail.value
    var enterprise_uid = that.data.zphid

    console.log('enterprise_uid',enterprise_uid)
    console.log('执行search',status)
    if(status == ''){
      return false
    }
    wx.request({
      header: {
        'content-type': 'application/json' // 默认值
      },
      url:url+ 'detail/getAllJFEnterpriseDetailPositionByPage',
      method: "get",
      data:{
        jfId :enterprise_uid,
        pageNum:1,
        pageSize:1150,
        position_name:status,//职位名称
      },
      success:function(res){
        console.log("搜索",res);
        var ret = res.data
        if(ret.code == 200){
          var listdata = ret.data.content
          var num = ret.data.content.length
          that.setData({listdata:listdata,num:num})
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  daohang(e){
    var lat = e.currentTarget.dataset.lat
    var lng = e.currentTarget.dataset.lon
    var store = e.currentTarget.dataset.store
    var address = e.currentTarget.dataset.address
    console.log(e)
    wx.getLocation({
        type: 'wgs84',
        success: function (res) {
            wx.openLocation({//​使用微信内置地图查看位置。
                latitude:  Number(lat),//要去的纬度-地址
                longitude: Number(lng),//要去的经度-地址
                name: store,
                address: address
            })
        },
        fail(res){
            if(res.errCode == 2){
                wx.showToast({
                  title: '定开启定位...',
                  icon:'none',
                  duration:1500,
                })
            }
            console.log('124312',res)
        },
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  prisedetail(e){
    let pid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/HomePage/prisedetail/prisedetail?id='+pid,
    })
  },
})